local boxielong = fk.CreateSkill {
  name = "boxielong",
}

Fk:loadTranslationTable{
  ["boxielong"] = "挟龙",
  [":boxielong"] = "出牌阶段限一次，你可以选择一名其他角色并进行判定，若结果不为红桃，则该角色的回合改为由你操控。",
  ["#boxielong"] = "挟龙：选择一名其他角色进行判定并操控",
  ["#boxielong-fail"] = "挟龙判定失败：结果为红桃，无法操控",
  ["$boxielong1"] = "日月之行，若出其中。",
  ["$boxielong2"] = "星汉灿烂，若出其里。",
}

boxielong:addEffect("active", {
  anim_type = "control",
  prompt = "#boxielong",
  card_num = 0,
  target_num = 1,
  
  can_use = function(self, player)
    return player:usedSkillTimes(boxielong.name, Player.HistoryPhase) == 0
  end,
  
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  
  on_target_selected = function(self, room, effect)
    if #effect.tos ~= 1 then
      return false
    end
    return true
  end,
  
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    
    -- 创建判定对象，直接匹配非红桃的三个花色
    local judge = {
      who = player,
      reason = boxielong.name,
      pattern = ".|.|spade,diamond,club", -- 匹配黑桃、方块、梅花
    }
    
    -- 执行判定
    room:judge(judge)
    
    -- 判断判定结果
    if judge:matchPattern() then
      -- 判定结果为非红桃（黑桃、方块、梅花），执行操控
      room:addPlayerMark(target, "boxielongcontrolled", 1)
      player:control(target)
    end
  end
})

-- 自动恢复机制（回合结束时自动恢复）
boxielong:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return target:getMark("boxielongcontrolled") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    -- 清除操控标记
    player.room:setPlayerMark(target, "boxielongcontrolled", 0)
    
    -- 恢复自我操控
    target:control(target)
  end,
})

return boxielong
